package com.sword.controller.stock;

import com.sword.constant.MessageConstant;
import com.sword.page.PageResult;
import com.sword.page.QueryPageBean;
import com.sword.page.Result;
import com.sword.pojo.stock.BackstockDetail;
import com.sword.pojo.stock.Stockpile;
import com.sword.service.impl.stock.StockpileServiceImpl;
import com.sword.service.stock.StockpileService;
import com.sword.service.stock.product.ProductService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/stock")
public class StockPileController {
    @Autowired
    private  StockpileService stockpileService;


    @RequestMapping("stockpile")
    public String stockpile() {
        return "views/stock/stockpile";
    }

    /**
   查询所有库存
    */
    @ResponseBody
    @RequestMapping("getStockpiles")
    public PageResult queryStockPiles(@RequestBody QueryPageBean queryPageBean) {
        try {
            return stockpileService.queryStockPile(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(500, MessageConstant.QUERY_STOCKPILE_FAIL, null, null);
        }
    }

    /**
    添加库存
     */
    @ResponseBody
    @RequestMapping("addStockpile")
    public Result addStockPiles(@RequestBody Stockpile stockpile) {
        try {
            // 获取session保存进数据
            String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
            stockpile.setCreateid(userId);

            // 判断是否添加成功
            Integer flag = stockpileService.addStockPile(stockpile);
            if (flag <= 0) {
                return new Result(500, MessageConstant.ADD_STOCKPILE_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.ADD_STOCKPILE_FAIL, null);
        }
        return new Result(200, MessageConstant.ADD_STOCKPILE_SUCCESS, null);
    }
    /**
    修改库存信息
     */
    @ResponseBody
    @RequestMapping("editStockpile")
    public Result editStockPiles(@RequestBody Stockpile stockpile) {
        try {
            Integer flag = stockpileService.editStockPile(stockpile);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_STOCKPILE_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_STOCKPILE_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_STOCKPILE_SUCCESS, null);
    }
    /**
     * 审核库存明细
     */
    @ResponseBody
    @RequestMapping("examinestockpile")
    public Result examinestockpile(@RequestBody Stockpile stockpile) {
        try {
            Integer flag = stockpileService.examineStockPile(stockpile);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_STOCKPILE_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_STOCKPILE_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_STOCKPILE_SUCCESS, null);
    }
    /**
     * 弃审库存明细
     */
    @ResponseBody
    @RequestMapping("unexaminestockpile")
    public Result unexaminestockpile(@RequestBody Stockpile stockpile) {
        try {
            Integer flag = stockpileService.unexamineStockPile(stockpile);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_STOCKPILE_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_STOCKPILE_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_STOCKPILE_SUCCESS, null);
    }
    /**
     * 删除库存
     */

    @ResponseBody
    @RequestMapping("/stock/deleteStockpile")
    public Result deleteStockPiles(Integer id) {
        try {
            Integer flag = stockpileService.deleteStockPile(id);
            if (flag <= 0) {
                return new Result(500, MessageConstant.DELETE_STOCKPILE_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.DELETE_STOCKPILE_FAIL, null);
        }
        return new Result(200, MessageConstant.DELETE_STOCKPILE_SUCCESS, null);
    }

}
